﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Module_SelectFlight : PanelControl
{
    protected string ngaydi, ngayve, noidi, noiden, khuhoi, motchieu;
    FlightBusiness fb = new FlightBusiness();

    protected void Page_Load(object sender, EventArgs e)
    {
        ngaydi = Helper.getQuerystr("ngaydi");
        ngayve = Helper.getQuerystr("ngayve");
        noidi = Helper.getQuerystr("noidi");
        noiden = Helper.getQuerystr("noiden");
        khuhoi = Helper.getQuerystr("khuhoi");
        motchieu = Helper.getQuerystr("motchieu");
        if (ngaydi == null && noidi == null && noiden == null && khuhoi == null)
        {
            BookingEntities be = new BookingEntities();
            be = (BookingEntities)Session["datve"];
            ngaydi = be._ngaydi;
            noidi = be._noidi;
            noiden = be._noiden;
            khuhoi = be._khuhoi;
        }
        
        if(khuhoi=="False"){
            this.an1.Visible = false;
            this.an2.Visible = false;
            this.an3.Visible = false;
        }
    }
    public string titDeparture()
    {
        
        return "Đi từ "+noidi+", "+"đến"+noiden +", "+ngaydi;
    }

    public string titArrival()
    {
        
        return "Đi từ " + noiden + ", " + "đến" + noidi + ", " + ngayve;
    }

    public string selectFlightDepartureDate()
    {
        string tmp = "";
        
        if (ngaydi != "" || ngaydi != null)
        {            
            double i = (0 - 3);
            DateTime ty = Convert.ToDateTime(ngaydi).AddDays(+3);
            while (Convert.ToDateTime(ngaydi).AddDays(i) <= Convert.ToDateTime(ngaydi).AddDays(+3))
            {
                DateTime t = Convert.ToDateTime(ngaydi).AddDays(i);
                DataTable dt = fb.searchFlight(noidi, noiden, t.ToShortDateString());
                if (dt != null)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        DataRow dr = dt.Rows[j];
                        tmp += "<td>";
                        tmp += "<a href=\"Default.aspx?url=Module/SelectFlight.ascx&ngaydi=" + Convert.ToDateTime(dr["departureDate"]).ToShortDateString() + "&ngayve=" + this.ngayve + "&noidi=" + noidi + "&noiden=" + noiden + "&khuhoi=" + khuhoi + "&motchieu=" + motchieu + "\">" + Convert.ToDateTime(dr["departureDate"]).ToShortDateString() + "<br/>" + Convert.ToDateTime(dr["departureDate"]).DayOfWeek.ToString() + "</a>";
                        //tmp += "<input type=\"radio\"  name=\"" + rdoName + "\"  value=\"" + dr["departureDate"] + "\" onclick=\"rdoClick(value)\"/>";
                        tmp += "</td>";
                    }
                }
                i++;
            }
        }
        return tmp;
    }

    public string selectFlightArrivalDate()
    {
        string tmp = "";
        string ngaydi = ngayve;
        
        {
            double i = (0 - 3);
            DateTime ty = Convert.ToDateTime(ngaydi).AddDays(+3);
            while (Convert.ToDateTime(ngaydi).AddDays(i) <= Convert.ToDateTime(ngaydi).AddDays(+3))
            {
                DateTime t = Convert.ToDateTime(ngaydi).AddDays(i);
                DataTable dt = fb.searchFlight(noidi, noiden, t.ToShortDateString());
                if (dt != null)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        DataRow dr = dt.Rows[j];
                        tmp += "<td>";
                        tmp += "<a href=\"Default.aspx?url=Module/SelectFlight.ascx&ngayve=" + Convert.ToDateTime(dr["departureDate"]).ToShortDateString() + "&ngaydi=" + this.ngaydi + "&noidi=" + noidi + "&noiden=" + noiden + "&khuhoi=" + khuhoi + "&motchieu=" + motchieu + "\">" + Convert.ToDateTime(dr["departureDate"]).ToShortDateString() + "<br/>" + Convert.ToDateTime(dr["departureDate"]).DayOfWeek.ToString() + "</a>";
                        //tmp += "<input type=\"radio\"  name=\"" + rdoName + "\"  value=\"" + dr["departureDate"] + "\" onclick=\"rdoClick(value)\"/>";
                        tmp += "</td>";
                    }
                }
                i++;
            }
        }
        return tmp;
    }

    public string titClass()
    {
        string tmp="";
        ClassBusiness cb = new ClassBusiness();
        DataTable dt = cb.getAllClass();
        if (dt != null)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                tmp += "<td>";
                tmp += dr["className"];
                tmp += "</td>";
            }
        }
        return tmp;
    }

    public string FlightDetailDeparture(string rdo)
    {
        string tmp = "";
        DataTable dt;
        DateTime t ;
        
            t = Convert.ToDateTime(ngaydi);
            dt = fb.getFlightDetail(noidi, noiden, t.ToShortDateString());
        
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            tmp += "<tr>";
            tmp += "<td>";
            tmp += dr["departure"] + "<br/>" + dr["startTime"];
            tmp += "</td>";
            tmp += "<td>";
            tmp += dr["arrival"] + "<br/>" + dr["endTime"];
            tmp += "</td>";
            tmp += "<td>";
            tmp += "VN"+dr["flightID"] + "<br/>" + dr["planeSymbol"];
            tmp += "</td>";
            ClassBusiness cb = new ClassBusiness();
            DataTable dtClass = cb.getAllClass();
            if (dtClass != null)
            {
                for (int j = 0; j < dtClass.Rows.Count; j++)
                {
                    DataRow drClass = dtClass.Rows[j];
                    tmp += "<td>";
                    tmp += "<input type=\"radio\" name=\"" + rdo + "\" value=\"" + dr["flightID"] + "," + drClass["classID"] + "\">";
                    tmp += "</td>";
                }
            }
            tmp += "</tr>";
        }
            return tmp;
    }

    public string FlightDetailArrival(string rdo)
    {
        string tmp = "";
        DataTable dt;
        DateTime t;
        
            t = Convert.ToDateTime(ngayve);
            dt = fb.getFlightDetail(noidi, noiden, t.ToShortDateString());
        
            
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            tmp += "<tr>";
            tmp += "<td>";
            tmp += dr["departure"] + "<br/>" + dr["startTime"];
            tmp += "</td>";
            tmp += "<td>";
            tmp += dr["arrival"] + "<br/>" + dr["endTime"];
            tmp += "</td>";
            tmp += "<td>";
            tmp += "VN" + dr["flightID"] + "<br/>" + dr["planeSymbol"];
            tmp += "</td>";
            ClassBusiness cb = new ClassBusiness();
            PlaneBusiness pb = new PlaneBusiness();
            DataTable dtClass = cb.getAllClass();
            AirfareBusiness ab = new AirfareBusiness();
            if (dtClass != null)
            {
                for (int j = 0; j < dtClass.Rows.Count; j++)
                {
                    DataRow drClass = dtClass.Rows[j];
                    PlaneEntities pe = pb.getSeatOfPlane(int.Parse(dr["planeID"].ToString()), int.Parse(drClass["classID"].ToString()));
                    int amount = pe._planeSeat;
                    int count = ab.coutAirfare(int.Parse(dr["flightID"].ToString()), int.Parse(drClass["classID"].ToString()));
                    tmp += "<td>";
                    if ((amount - count) > 0)
                        tmp += "<input type=\"radio\" name=\"" + rdo + "\" value=\"" + dr["flightID"] + "," + drClass["classID"] + "\" >";
                    else
                        tmp += "Đã hết chỗ";
                    tmp += "</td>";
                }
            }
            tmp += "</tr>";
        }
        return tmp;
    }

    
}